package love.yuanmengrobot.als.system.servlet;

import love.yuanmengrobot.als.system.dao.impl.AnnounccementDaoImpl;
import love.yuanmengrobot.als.system.dao.impl.MainPageContentDaoImpl;
import love.yuanmengrobot.als.system.javaBean.Alsannouncement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

public class AnnouncementServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String id = request.getParameter("annoucementId");
        String op = request.getParameter("op");
        String url = request.getRequestURI();

        System.out.println(op + "*****" + url + "****" + id);

        if (isnumber(op)) {
            Alsannouncement alsannouncement = null;

            //op---增0删1改2
            switch (Integer.parseInt(op)) {
                case 0:
                    System.out.println("增加公告");
                    alsannouncement = new Alsannouncement();
                    alsannouncement.setTitile(request.getParameter("AnnouncementTitle"));
                    alsannouncement.setNotice_content(request.getParameter("AnnouncementContent"));
                    alsannouncement.setPublisher(request.getParameter("AnnouncementFooterPublisher"));
                    boolean addS = addAnnouncement(alsannouncement);
                    request.setAttribute("add", addS);
                    toView(request, response);
                    break;
                case 1:
                    System.out.println("删除公告");
                    if (isnumber(id)) {
                        boolean deleteS = deleteAnnouncement(id);
                        request.setAttribute("delete", deleteS);
                    } else {
                        request.setAttribute("delete", false);
                    }
                    toView(request, response);
                    break;
                case 2:
                    System.out.println("修改公告");
                    alsannouncement = new Alsannouncement();
                    alsannouncement.setNoticeID(request.getParameter("AnnouncementId"));
                    alsannouncement.setTitile(request.getParameter("AnnouncementTitle"));
                    alsannouncement.setNotice_content(request.getParameter("AnnouncementContent"));
                    boolean updateS = updateAnnouncement(alsannouncement);
                    System.out.println("结果："+updateS);
                    request.setAttribute("update", updateS);
                    toView(request, response);
                    break;

                default:

            }


        }


    }

    //get是获取公告数据
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        if (request.getSession().getAttribute("alsUser") == null) {
//            System.out.println("用户未登录！");
//        }
        toView(request, response);
    }

    private static boolean isnumber(String num) {
        if (num == null || "".equals(num)) return false;
        Pattern pt = Pattern.compile("^[-+]?[\\d]*$");
        return pt.matcher(num).matches();
    }

    /*
     * 删除公告
     * @return 查询true-成功，false-失败
     * @公告id*/
    public boolean deleteAnnouncement(String id) {

        AnnounccementDaoImpl announccementDao = new AnnounccementDaoImpl();
        boolean su = announccementDao.deleteAnnouncement(id);
        return su;
    }

    /*
     * 添加公告
     * @return true-成功，false-失败
     * @公告类*/
    public boolean addAnnouncement(Alsannouncement announcement) {
        System.out.println("获取：" + announcement.toString());
        AnnounccementDaoImpl announccementDao = new AnnounccementDaoImpl();
        return announccementDao.addAnnouncement(announcement);
    }

    /*
     * 修改公告
     * @return true-成功，false-失败
     * @公告类*/
    public boolean updateAnnouncement(Alsannouncement announcement) {
        System.out.println("获取：" + announcement.toString());

        AnnounccementDaoImpl announccementDao = new AnnounccementDaoImpl();
        return announccementDao.updateAnnouncement(announcement);
    }

    /*
     * 跳转到公告
     * @return
     * @*/
    public void toView(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MainPageContentDaoImpl mainPageContentDao = new MainPageContentDaoImpl();
        List<Alsannouncement> announcement = mainPageContentDao.selectAllAnnouncement();
        request.setAttribute("announcement", announcement);
        request.getRequestDispatcher("WEB-INF/view/system/announcement/annoucement.jsp").forward(request, response);

    }

}
